<template>
    <div class="pb_a">
        <van-nav-bar class="head_nb" title="每天一练"/>
        <div class="pt_a">
            <!--未登录-->
            <div v-if="user.userId==''">
                <!--所有科目每天一练-->
                <van-cell-group v-for="item in subjects" :title="item.name" :key="item.id">
                    <van-cell :title="item.name+'每日一练（综合）'" is-link to="index"/>
                    <van-cell :title="item.name+'每日一练（案例）'" is-link to="index"/>
                    <van-cell :title="item.name+'每日一练（汇总）'" is-link to="index"/>
                </van-cell-group>
            </div>
            <!--已登录-->
            <div v-if="user.userId!=''">
                <div class="ft_14 txt_cr txt_40" style="position:absolute;right:1rem;" @click="$router.push('/subject');">
                    <span>
                        切换科目<van-icon name="arrow" size="1rem" class="icon_mid"/>
                    </span>
                </div>
                <!--已选科目每天一练-->
                <van-cell-group v-for="item in subjects" :title="item.name" :key="item.id">
                    <van-cell :title="item.name+'每日一练（综合）'" is-link to="index"/>
                    <van-cell :title="item.name+'每日一练（案例）'" is-link to="index"/>
                    <van-cell :title="item.name+'每日一练（汇总）'" is-link to="index"/>
                </van-cell-group>
            </div>
        </div>
    </div>
</template>

<script>
    import Vue from 'vue';
    import {NavBar} from 'vant';
    import common from '../../common/common'
    import {mapActions, mapState} from 'vuex';
    import {getSubject} from '../../api';

    Vue.use(NavBar);
    export default {
        name: "Daily",
        data() {
            return {
                subjects: [],
            };
        },
        async created() {
            //异步获取用户选择的科目
            const user = this.user;
            const subject = this.subject;
            if (user.userId != '') {
                await this.reqUserSubject();
                const subjectId = subject.subjectId || '';
                if (subjectId == '') {
                    //先去到选择科目界面
                    common.toast("fail", '您还未选择科目，请先选择再操作！', () => {
                        this.$router.push('/subject');
                    });
                }
            }

            //异步获取所有的科目
            const params = {'id': subject.subjectId};
            const result = await getSubject(params);
            if (result.code === 0) {
                this.subjects = result.data;
            }
        },
        computed: {
            ...mapState(['user', 'subject']),
        },
        methods: {
            ...mapActions(['reqUserSubject']),
        }
    }
</script>

<style scoped>

</style>
